﻿using PP.PaintLib;
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace MyPaint
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //启动程序
            var progress1=GetExistProcess();
            if (progress1 != null)
            {
                IntPtr mainWindowHandle1 = progress1.MainWindowHandle;
                if (mainWindowHandle1 == IntPtr.Zero)//窗口被隐藏了
                {
                    //目标窗体的 Text 属性中的字符串
                    mainWindowHandle1 = PPWin32.FindWindow(null, "PP画图");
                }
                PPWin32.ShowWindowAsync(mainWindowHandle1, 1);
                PPWin32.SetForegroundWindow(mainWindowHandle1);
            }
            else
            {
                Application.Run(new MainForm());
            }
        }

        /// <summary>
        /// 查看程序是否已经运行
        /// </summary>
        /// <returns></returns>
        private static Process GetExistProcess()
        {
            Process currentProcess = Process.GetCurrentProcess();
            foreach (Process process1 in Process.GetProcessesByName(currentProcess.ProcessName))
            {
                if (process1.Id != currentProcess.Id)
                {
                    return process1;
                }
            }
            return null;
        }
    }
}
